﻿using System;
using System.Deployment.Application;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Linq;
using Microsoft.Win32;

namespace WindowsPhone.ScreenshotPowerTools
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Title = Properties.Resources.AppName;

            ViewModel = new MainViewModel();

            this.AppNameElement.Text = Properties.Resources.AppName;
            Version version;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
            }
            else
            {
                version = Assembly.GetExecutingAssembly().GetName().Version;
            }
            this.VersionElement.Text = string.Format(CultureInfo.CurrentCulture, Properties.Resources.VersionFormat, version);
        }

        private MainViewModel _viewModel;
        private MainViewModel ViewModel
        {
            get { return _viewModel; }
            set
            {
                if (_viewModel != value)
                {
                    _viewModel = value;
                    DataContext = _viewModel;
                }
            }
        }

        private void ChooseButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = Properties.Resources.PngFilter,
                Multiselect = true,
                Title = Properties.Resources.ChooseScreenshotsDialogTitle
            };
            if (dialog.ShowDialog() == true)
            {
                ViewModel.Screenshots.Clear();
                foreach (var fileName in dialog.FileNames)
                {
                    ViewModel.Screenshots.Add(fileName);
                }
            }
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(e.Uri.ToString());
        }
    }
}
